"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _createListComponent = _interopRequireWildcard(require("./createListComponent"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

var devWarningsDirection = null;
var devWarningsTagName = null;

if (process.env.NODE_ENV !== 'production') {
  if (typeof window !== 'undefined' && typeof window.WeakSet !== 'undefined') {
    devWarningsDirection = /* #__PURE__ */new WeakSet();
    devWarningsTagName = /* #__PURE__ */new WeakSet();
  }
}

var FixedSizeList = /* #__PURE__ */(0, _createListComponent.default)({
  getItemOffset: function getItemOffset(props, index, ref) {
    if (!props.unlimitedSize) {
      return index * props.itemSize;
    }

    return ref._getCountSize(props, index);
  },
  getItemSize: function getItemSize(props, index, ref) {
    if (!props.unlimitedSize) {
      return props.itemSize;
    }

    return ref._getSizeUpload(index, (0, _createListComponent.isHorizontalFunc)(props));
  },
  getEstimatedTotalSize: function getEstimatedTotalSize(props, ref) {
    return ref._getCountSize(props, props.itemCount);
  },
  getOffsetForIndexAndAlignment: function getOffsetForIndexAndAlignment(props, id, index, align, scrollOffset, ref) {
    var height = props.height,
        width = props.width;
    var sizeList = ref.state.sizeList; // TODO Deprecate direction "horizontal"

    var size = (0, _createListComponent.isHorizontalFunc)(props) ? width : height;

    var itemSize = ref._getSize(sizeList[index]);

    var lastItemOffset = Math.max(0, ref._getCountSize(props, props.itemCount) - size);
    var maxOffset = Math.min(lastItemOffset, ref._getCountSize(props, index));
    var minOffset = Math.max(0, ref._getCountSize(props, index) - size + itemSize);

    if (align === 'smart') {
      if (scrollOffset >= minOffset - size && scrollOffset <= maxOffset + size) {
        align = 'auto';
      } else {
        align = 'center';
      }
    }

    switch (align) {
      case 'start':
        return maxOffset;

      case 'end':
        return minOffset;

      case 'center':
        {
          // "Centered" offset is usually the average of the min and max.
          // But near the edges of the list, this doesn't hold true.
          var middleOffset = Math.round(minOffset + (maxOffset - minOffset) / 2);

          if (middleOffset < Math.ceil(size / 2)) {
            return 0; // near the beginning
          }

          if (middleOffset > lastItemOffset + Math.floor(size / 2)) {
            return lastItemOffset; // near the end
          }

          return middleOffset;
        }

      case 'auto':
      default:
        if (scrollOffset >= minOffset && scrollOffset <= maxOffset) {
          return scrollOffset;
        }

        if (scrollOffset < minOffset) {
          return minOffset;
        }

        return maxOffset;
    }
  },
  getStartIndexForOffset: function getStartIndexForOffset(props, scrollOffset, ref) {
    return Math.max(0, ref._getSizeCount(props, scrollOffset) - 1);
  },
  getStopIndexForStartIndex: function getStopIndexForStartIndex(props, scrollOffset, startIndex, ref) {
    var height = props.height,
        itemCount = props.itemCount,
        itemSize = props.itemSize,
        width = props.width;
    var size = (0, _createListComponent.isHorizontalFunc)(props) ? width : height;

    var offset = ref._getCountSize(props, startIndex);

    if (!props.unlimitedSize) {
      // TODO Deprecate direction "horizontal"
      var numVisibleItems = Math.ceil((size + scrollOffset - offset) / itemSize);
      /** -1 is because stop index is inclusive */

      return Math.max(startIndex, Math.min(itemCount - 1, startIndex + numVisibleItems - 1));
    }

    return Math.max(startIndex, Math.min(itemCount - 1, ref._getSizeCount(props, size + scrollOffset)));
  },
  initInstanceProps: function initInstanceProps() {// Noop
  },
  shouldResetStyleCacheOnItemSizeChange: true,
  validateProps: function validateProps(nextProps, prevState) {
    var itemCount = nextProps.itemCount,
        itemSize = nextProps.itemSize;
    var sizeList = prevState.sizeList;

    if (itemCount > sizeList.length) {
      var arr = new Array(itemCount - sizeList.length).fill(-1);
      sizeList.push.apply(sizeList, _toConsumableArray(arr));
    } else if (itemCount < sizeList.length) {
      sizeList.length = itemCount;
    }

    if (process.env.NODE_ENV !== 'production') {
      if (typeof itemSize !== 'number') {
        throw Error('An invalid "itemSize" prop has been specified. ' + 'Value should be a number. ' + "\"".concat(itemSize === null ? 'null' : _typeof(itemSize), "\" was specified."));
      }
    }

    validateSharedProps(nextProps, prevState);
  }
});

var validateSharedProps = function validateSharedProps(_ref, _ref2) {
  var children = _ref.children,
      direction = _ref.direction,
      height = _ref.height,
      layout = _ref.layout,
      itemTagName = _ref.itemTagName,
      innerTagName = _ref.innerTagName,
      outerTagName = _ref.outerTagName,
      width = _ref.width;
  var instance = _ref2.instance;

  if (process.env.NODE_ENV !== 'production') {
    if (innerTagName != null || outerTagName != null || itemTagName != null) {
      if (devWarningsTagName && !devWarningsTagName.has(instance)) {
        devWarningsTagName.add(instance);
        console.warn('The itemTagName、innerTagName and outerTagName props have been deprecated. ' + 'Please use the itemElementType、innerElementType and outerElementType props instead.');
      }
    } // TODO Deprecate direction "horizontal"


    var isHorizontal = direction === 'horizontal' || layout === 'horizontal';

    switch (direction) {
      case 'horizontal':
      case 'vertical':
        if (devWarningsDirection && !devWarningsDirection.has(instance)) {
          devWarningsDirection.add(instance);
          console.warn('The direction prop should be either "ltr" (default) or "rtl". ' + 'Please use the layout prop to specify "vertical" (default) or "horizontal" orientation.');
        }

        break;

      case 'ltr':
      case 'rtl':
        // Valid values
        break;

      default:
        throw Error('An invalid "direction" prop has been specified. ' + 'Value should be either "ltr" or "rtl". ' + "\"".concat(direction, "\" was specified."));
    }

    switch (layout) {
      case 'horizontal':
      case 'vertical':
        // Valid values
        break;

      default:
        throw Error('An invalid "layout" prop has been specified. ' + 'Value should be either "horizontal" or "vertical". ' + "\"".concat(layout, "\" was specified."));
    }

    if (children == null) {
      throw Error('An invalid "children" prop has been specified. ' + 'Value should be a React component. ' + "\"".concat(children === null ? 'null' : _typeof(children), "\" was specified."));
    }

    if (isHorizontal && typeof width !== 'number') {
      throw Error('An invalid "width" prop has been specified. ' + 'Horizontal lists must specify a number for width. ' + "\"".concat(width === null ? 'null' : _typeof(width), "\" was specified."));
    } else if (!isHorizontal && typeof height !== 'number') {
      throw Error('An invalid "height" prop has been specified. ' + 'Vertical lists must specify a number for height. ' + "\"".concat(height === null ? 'null' : _typeof(height), "\" was specified."));
    }
  }
};

var _default = FixedSizeList;
exports.default = _default;